let userData = [
  {id: '001', name: 'admin', pass: '123abc', roleType: 1},
  {id: '002', name: 'lp', pass: '123456', roleType: 2},
  {id: '003', name: 'lp1', pass: '123456', roleType: 2},
  {id: '004', name: 'lp2', pass: '123456', roleType: 2},
  {id: '005', name: 'lp3', pass: '123456', roleType: 2},
  {id: '006', name: 'lp4', pass: '123456', roleType: 2},
  {id: '007', name: 'lp5', pass: '123456', roleType: 2},
  {id: '008', name: 'lp6', pass: '123456', roleType: 2},
  {id: '009', name: 'lp7', pass: '123456', roleType: 2},
  {id: '010', name: 'lp8', pass: '123456', roleType: 2},
  {id: '011', name: 'lp9', pass: '123456', roleType: 2}
]
// 登录验证
export function loginVerify (data) {
  console.log(userData)
  let info = ''
  userData.forEach((item, index) => {
    if (item.name === data.name && item.pass === data.pass) {
      info = item
    }
  })
  return info
}
// 拿到cookie的值，传参传要拿哪个cookie
export function getCookie (param) {
  var strcookie = document.cookie // 获取cookie字符串
  var arrcookie = strcookie.split('; ') // 分割
  // 遍历匹配
  for (var i = 0; i < arrcookie.length; i++) {
    var arr = arrcookie[i].split('=')
    if (arr[0] === param) {
      return arr[1]
    }
  }
  return ''
}
// 根据id来拿其他的值
export function searchUserInfo (id) {
  var list = ''
  userData.forEach((item, index) => {
    if (item.id === id) {
      list = item
      // list.pass = undefined
    }
  })
  return list
}
// 根据id来删除用户
export function deleteUser (id) {
  userData.forEach((item, index) => {
    if (item.id === id) {
      delete userData[index]
    }
  })
  return true
}
// 根据id来删除用户
export function resetUserPass (id) {
  userData.forEach((item, index) => {
    if (item.id === id) {
      item.pass = '123456'
    }
  })
  return true
}
// 返回用户列表
export function getUserList (page, pageSize, searchData) {
  var length
  var tableData = {
    data: [],
    page,
    pageSize,
    length
  }
  if (searchData instanceof Object && Object.keys(searchData).length) {
    let list = []
    for (let i in searchData) {
      console.log(i)
      console.log(searchData[i])
      if (searchData[i]) {
        list = userData.filter(item => item[i].includes(searchData[i]))
      }
    }
    tableData.data = list
    length = tableData.data.length
  } else {
    console.log('传值为null')
    tableData.data = userData
  }
  if (pageSize >= length) { // pageSize大于等于总数据长度，说明只有1页数据或没有数据
    tableData.length = length
    tableData.page = page// 直接取第一页
  } else { // pageSize小于总数据长度，数据多余1页
    const num = pageSize * (page - 1)// 计算当前页（不含）之前的所有数据总条数
    if (num < length) { // 如果当前页之前所有数据总条数小于（不能等于）总的数据集长度，则说明当前页码没有超出最大页码
      const startIndex = num// 当前页第一条数据在总数据集中的索引
      const endIndex = num + pageSize - 1// 当前页最后一条数据索引
      tableData.data = userData.filter((_, index) => index >= startIndex && index <= endIndex)// 当前页数据条数小于每页最大条数时，也按最大条数范围筛取数据
    } else { // 当前页码超出最大页码，则计算实际最后一页的page，自动返回最后一页数据
      const size = parseInt(length / pageSize) // 取商
      const rest = length % pageSize // 取余数
      if (rest > 0) { // 余数大于0，说明实际最后一页数据不足pageSize，应该取size+1为最后一条的页码
        tableData.page = size + 1// 当前页码重置，取size+1
        tableData.data = userData.filter((_, index) => index >= (pageSize * size) && index <= length)
      } else if (rest === 0) { // 余数等于0，最后一页数据条数正好是pageSize
        tableData.page = size// 当前页码重置，取size
        tableData.data = userData.filter((_, index) => index >= (pageSize * (size - 1)) && index <= length)
      } // 注：余数不可能小于0
    }
  }
  console.log(tableData)
  return tableData
}
